From 835e5ac1acf7fbf120c2d6aaa7ca30d9d5b30987 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 16 Jul 2014 18:44:30 -0700 Subject: [PATCH] Fix inferred name of `src/main.rs` The crate name should be the package name, not `main`. Closes #207 --- src/cargo/ops/cargo_run.rs | 8 +++++++- src/cargo/sources/git/utils.rs | 3 +-- src/cargo/util/toml.rs | 5 ++++- tests/support/mod.rs | 6 ++---- tests/test_cargo_compile.rs | 16 ++++++++++++++++ tests/test_cargo_compile_git_deps.rs | 4 ++-- tests/test_cargo_cross_compile.rs | 12 ++++++------ tests/test_cargo_run.rs | 4 ++-- tests/test_cargo_test.rs | 5 +++++ tests/test_shell.rs | 3 +-- 10 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/cargo/ops/cargo_run.rs b/src/cargo/ops/cargo_run.rs index 6b6198a0d..dcf2e1ab7 100644 --- a/src/cargo/ops/cargo_run.rs +++ b/src/cargo/ops/cargo_run.rs @@ -2,6 +2,8 @@ use std::os; use ops; use util::{CargoResult, human, process, ProcessError}; +use core::source::Source; +use sources::PathSource; pub fn run(manifest_path: &Path, options: &mut ops::CompileOptions, @@ -10,8 +12,12 @@ pub fn run(manifest_path: &Path, return Err(human("`src/main.rs` must be present for `cargo run`")) } + let mut src = PathSource::for_path(&manifest_path.dir_path()); + try!(src.update()); + let root = try!(src.get_root_package()); + try!(ops::compile(manifest_path, options)); - let exe = manifest_path.dir_path().join("target/main"); + let exe = manifest_path.dir_path().join("target").join(root.get_name()); let exe = match exe.path_relative_from(&os::getcwd()) { Some(path) => path, None => exe, diff --git a/src/cargo/sources/git/utils.rs b/src/cargo/sources/git/utils.rs index e90c49e4a..b8c0ef15a 100644 --- a/src/cargo/sources/git/utils.rs +++ b/src/cargo/sources/git/utils.rs @@ -1,6 +1,5 @@ use std::fmt; use std::fmt::{Show,Formatter}; -use std::str; use std::io::{UserDir}; use std::io::fs::{mkdir_recursive,rmdir_recursive}; use serialize::{Encodable,Encoder}; @@ -311,6 +310,6 @@ fn git_output(path: &Path, str: String) -> CargoResult { } fn to_str(vec: &[u8]) -> String { - str::from_utf8_lossy(vec).to_string() + String::from_utf8_lossy(vec).into_string() } diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index a2fd7b369..96b43f493 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -17,6 +17,7 @@ use util::{CargoResult, Require, human}; #[deriving(Clone)] pub struct Layout { + root: Path, lib: Option, bins: Vec, examples: Vec, @@ -70,6 +71,7 @@ pub fn project_layout(root_path: &Path) -> Layout { try_add_files(&mut tests, root_path, "tests"); Layout { + root: root_path.clone(), lib: lib, bins: bins, examples: examples, @@ -237,7 +239,8 @@ fn inferred_lib_target(name: &str, layout: &Layout) -> Option> { fn inferred_bin_targets(name: &str, layout: &Layout) -> Option> { Some(layout.bins.iter().filter_map(|bin| { - let name = if bin.as_str() == Some("src/main.rs") { + let name = if bin.as_vec() == b"src/main.rs" || + *bin == layout.root.join("src/main.rs") { Some(name.to_string()) } else { bin.filestem_str().map(|f| f.to_string()) diff --git a/tests/support/mod.rs b/tests/support/mod.rs index 302467362..1b025d815 100644 --- a/tests/support/mod.rs +++ b/tests/support/mod.rs @@ -1,5 +1,3 @@ -// use std::io::fs::{mkdir_recursive,rmdir_recursive}; -use std; use std::io; use std::io::fs; use std::io::process::{ProcessOutput}; @@ -305,7 +303,7 @@ impl Execs { `{}`\n\n\ other output:\n\ `{}`", description, actual, out, - str::from_utf8_lossy(extra))) + String::from_utf8_lossy(extra))) } } } @@ -358,7 +356,7 @@ impl<'a> ham::Matcher<&'a [u8]> for ShellWrites { -> ham::MatchResult { println!("{}", actual); - let actual = std::str::from_utf8_lossy(actual); + let actual = String::from_utf8_lossy(actual); let actual = actual.to_string(); ham::expect(actual == self.expected, actual) } diff --git a/tests/test_cargo_compile.rs b/tests/test_cargo_compile.rs index db114ac84..2a3290b2e 100644 --- a/tests/test_cargo_compile.rs +++ b/tests/test_cargo_compile.rs @@ -1209,3 +1209,19 @@ test!(release_build_ndebug { execs().with_status(0)); assert_that(process(p.bin("release/foo")), execs().with_stdout("fast\n")); }) + +test!(inferred_main_bin { + let p = project("world") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/main.rs", r#" + fn main() {} + "#); + + assert_that(p.cargo_process("cargo-build"), execs().with_status(0)); + assert_that(process(p.bin("foo")), execs().with_status(0)); +}) diff --git a/tests/test_cargo_compile_git_deps.rs b/tests/test_cargo_compile_git_deps.rs index b0ae8c267..3a0f52b2c 100644 --- a/tests/test_cargo_compile_git_deps.rs +++ b/tests/test_cargo_compile_git_deps.rs @@ -475,8 +475,8 @@ test!(two_revs_same_deps { // TODO: -j1 is a hack assert_that(foo.cargo_process("cargo-build").arg("-j").arg("1"), execs().with_status(0)); - assert_that(&foo.bin("main"), existing_file()); - assert_that(foo.process(foo.bin("main")), execs().with_status(0)); + assert_that(&foo.bin("foo"), existing_file()); + assert_that(foo.process(foo.bin("foo")), execs().with_status(0)); }) test!(recompilation { diff --git a/tests/test_cargo_cross_compile.rs b/tests/test_cargo_cross_compile.rs index e049cfd14..e7978e2a1 100644 --- a/tests/test_cargo_cross_compile.rs +++ b/tests/test_cargo_cross_compile.rs @@ -70,10 +70,10 @@ test!(simple_deps { let target = alternate(); assert_that(p.cargo_process("cargo-build").arg("--target").arg(target), execs().with_status(0)); - assert_that(&p.target_bin(target, "main"), existing_file()); + assert_that(&p.target_bin(target, "foo"), existing_file()); assert_that( - process(p.target_bin(target, "main")), + process(p.target_bin(target, "foo")), execs().with_status(0)); }) @@ -146,10 +146,10 @@ test!(plugin_deps { let target = alternate(); assert_that(foo.cargo_process("cargo-build").arg("--target").arg(target), execs().with_status(0)); - assert_that(&foo.target_bin(target, "main"), existing_file()); + assert_that(&foo.target_bin(target, "foo"), existing_file()); assert_that( - process(foo.target_bin(target, "main")), + process(foo.target_bin(target, "foo")), execs().with_status(0)); }) @@ -226,10 +226,10 @@ test!(plugin_to_the_max { let target = alternate(); assert_that(foo.cargo_process("cargo-build").arg("--target").arg(target), execs().with_status(0)); - assert_that(&foo.target_bin(target, "main"), existing_file()); + assert_that(&foo.target_bin(target, "foo"), existing_file()); assert_that( - process(foo.target_bin(target, "main")), + process(foo.target_bin(target, "foo")), execs().with_status(0)); }) diff --git a/tests/test_cargo_run.rs b/tests/test_cargo_run.rs index 355884e44..b4ec89c51 100644 --- a/tests/test_cargo_run.rs +++ b/tests/test_cargo_run.rs @@ -22,14 +22,14 @@ test!(simple { assert_that(p.cargo_process("cargo-run"), execs().with_status(0).with_stdout(format!("\ {compiling} foo v0.0.1 (file:{dir}) -{running} `target{sep}main` +{running} `target{sep}foo` hello ", compiling = COMPILING, running = RUNNING, dir = p.root().display(), sep = path::SEP).as_slice())); - assert_that(&p.bin("main"), existing_file()); + assert_that(&p.bin("foo"), existing_file()); }) test!(simple_with_args { diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index 7985d712f..17c551345 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -49,6 +49,10 @@ test!(test_with_lib_dep { name = "foo" version = "0.0.1" authors = [] + + [[bin]] + name = "baz" + path = "src/main.rs" "#) .file("src/lib.rs", " pub fn foo(){} @@ -81,6 +85,7 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured"; let head = format!("{compiling} foo v0.0.1 (file:{dir})", compiling = COMPILING, dir = p.root().display()); + println!("{}", out); assert!(out == format!("{}\n\n{}\n\n\n{}\n\n", head, bin, lib).as_slice() || out == format!("{}\n\n{}\n\n\n{}\n\n", head, lib, bin).as_slice()); }) diff --git a/tests/test_shell.rs b/tests/test_shell.rs index e7dc72eb7..d1211ab2b 100644 --- a/tests/test_shell.rs +++ b/tests/test_shell.rs @@ -1,7 +1,6 @@ use support::{ResultTest,Tap,shell_writes}; use hamcrest::{assert_that}; use std::io::{MemWriter, BufWriter, IoResult}; -use std::str::from_utf8_lossy; use cargo::core::shell::{Shell,ShellConfig}; use term::{Terminal,TerminfoTerminal,color}; @@ -56,5 +55,5 @@ fn colored_output(string: S, color: color::Color) -> IoResult { try!(term.write_str(string.as_slice())); try!(term.reset()); try!(term.flush()); - Ok(from_utf8_lossy(term.get_ref().get_ref()).to_string()) + Ok(String::from_utf8_lossy(term.get_ref().get_ref()).to_string()) } -- 2.30.2